{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Tce3stUlHN0L"
      },
      "source": [
        "##### Copyright 2020 The TensorFlow IO Authors."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "tuOe1ymfHZPu"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qFdPvlXBOdUN"
      },
      "source": [
        "# 色空間変換"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MfBg1C5NB3X0"
      },
      "source": [
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td><a target=\"_blank\" href=\"https://www.tensorflow.org/io/tutorials/colorspace\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\"> TensorFlow.orgで表示</a></td>\n",
        "  <td><a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs-l10n/blob/master/site/ja/io/tutorials/colorspace.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\"> Google Colab で実行</a></td>\n",
        "  <td><a target=\"_blank\" href=\"https://github.com/tensorflow/docs-l10n/blob/master/site/ja/io/tutorials/colorspace.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\">GitHub でソースを表示{</a></td>\n",
        "      <td><a href=\"https://storage.googleapis.com/tensorflow_docs/docs-l10n/site/ja/io/tutorials/colorspace.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\">ノートブックをダウンロード/a0}</a></td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xHxb-dlhMIzW"
      },
      "source": [
        "## 概要\n",
        "\n",
        "コンピュータビジョンでは、選択した色空間がモデルの性能を大きく左右することがあります。最も一般的な色空間は`RGB`ですが、多くの場合は`YUV`、`YCbCr`、`XYZ (CIE)`などの他の色空間に切り替えると、モデルの性能が向上します。\n",
        "\n",
        "`tensorflow-io`パッケージは、画像データの準備や拡張に使用できる色空間変換 API のリストを提供しています。"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MUXex9ctTuDB"
      },
      "source": [
        "## セットアップ"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "upgCc3gXybsA"
      },
      "source": [
        "### 必要なパッケージをインストールし、ランタイムを再起動する"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "uUDYyMZRfkX4"
      },
      "outputs": [],
      "source": [
        "!pip install tensorflow-io"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VSkY6UAxODOq"
      },
      "source": [
        "### サンプル画像をダウンロードする\n",
        "\n",
        "このチュートリアルで使用する画像例は[雪の中の猫](https://commons.wikimedia.org/wiki/File:Felis_catus-cat_on_snow.jpg)ですが、任意の JPEG 画像で置き換えても構いません。\n",
        "\n",
        "以下のように画像をダウンロードし、`sample.jpg`としてローカルディスクに保存します。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "baeqVsglPQUI"
      },
      "outputs": [],
      "source": [
        "!curl -o sample.jpg -L https://storage.googleapis.com/download.tensorflow.org/example_images/320px-Felis_catus-cat_on_snow.jpg\n",
        "\n",
        "!ls -ls sample.jpg"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "J0ZKhA6s0Pjp"
      },
      "source": [
        "## 使い方"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yZmI7l_GykcW"
      },
      "source": [
        "### 画像ファイルを読み込む\n",
        "\n",
        "画像を読み取り、形状が`(213, 320, 3)`の`uint8`テンソルにデコードします。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "nS3eTBvjt-O5"
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "import tensorflow_io as tfio\n",
        "\n",
        "image = tf.image.decode_jpeg(tf.io.read_file('sample.jpg'))\n",
        "\n",
        "print(image.shape, image.dtype)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IGnbXuVnSo8T"
      },
      "source": [
        "画像は以下の方法で表示できます。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "0rLbVxuFSvVO"
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n",
        "\n",
        "plt.figure()\n",
        "plt.imshow(image)\n",
        "plt.axis('off')\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "86qE8BPl5rcA"
      },
      "source": [
        "### RGB からグレースケールに変換する\n",
        "\n",
        "`tfio.experimental.color.rgb_to_grayscale`を使用して`RGB`画像を`Grayscale`に変換し、チャンネル数を 3 から 1 に減らすことができます。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "eEa0Z5U26Ep3"
      },
      "outputs": [],
      "source": [
        "grayscale = tfio.experimental.color.rgb_to_grayscale(image)\n",
        "\n",
        "print(grayscale.shape, grayscale.dtype)\n",
        "\n",
        "# use tf.squeeze to remove last channel for plt.imshow to display:\n",
        "plt.figure()\n",
        "plt.imshow(tf.squeeze(grayscale, axis=-1), cmap='gray')\n",
        "plt.axis('off')\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ineBzDeu-lTh"
      },
      "source": [
        "### RGB から BGR に変換する\n",
        "\n",
        "画像ソフトやカメラのメーカーによっては`BGR`を好む場合がありますが、`tfio.experimental.color.rgb_to_bgr`を使用して BGR に変換することができます。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "LfZo0XaaAaeM"
      },
      "outputs": [],
      "source": [
        "bgr = tfio.experimental.color.rgb_to_bgr(image)\n",
        "\n",
        "print(bgr.shape, bgr.dtype)\n",
        "\n",
        "plt.figure()\n",
        "plt.imshow(bgr)\n",
        "plt.axis('off')\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "nXd776xNIr_I"
      },
      "source": [
        "### RGB から CIE XYZ に変換する\n",
        "\n",
        "`CIE XYZ`（または `CIE 1931 XYZ`）は、多くの画像処理プログラムで使用されている一般的な色空間です。以下では`tfio.experimental.color.rgb_to__xyz`を使用して、RGB から`CIE XYZ`に変換しています。`tfio.experimental.color.rgb_to_xyz`は`[0, 1]`の範囲の浮動小数点入力を想定しているため、追加の前処理が必要なので注意してください。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "kLEdfkkoK27A"
      },
      "outputs": [],
      "source": [
        "# convert to float32\n",
        "image_float32 = tf.cast(image, tf.float32) / 255.0\n",
        "\n",
        "xyz_float32 = tfio.experimental.color.rgb_to_xyz(image_float32)\n",
        "\n",
        "# convert back uint8\n",
        "xyz = tf.cast(xyz_float32 * 255.0, tf.uint8)\n",
        "\n",
        "print(xyz.shape, xyz.dtype)\n",
        "\n",
        "plt.figure()\n",
        "plt.imshow(xyz)\n",
        "plt.axis('off')\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7rhLvOSZB0k0"
      },
      "source": [
        "### RGB から YCbCr に変換する\n",
        "\n",
        "最後に、多くのビデオシステムでは`YCbCr`がデフォルトの色空間です。`YCbCr`への変換は、`tfio.experimental.color.rgb_to_ycbcr`を使用して行います。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "UyFMBK-LDDnN"
      },
      "outputs": [],
      "source": [
        "ycbcr = tfio.experimental.color.rgb_to_ycbcr(image)\n",
        "\n",
        "print(ycbcr.shape, ycbcr.dtype)\n",
        "\n",
        "plt.figure()\n",
        "plt.imshow(ycbcr, cmap='gray')\n",
        "plt.axis('off')\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VuL8rrnhwab4"
      },
      "source": [
        "さらに面白いことに、`YCbCr`は各成分が知覚的に意味のある情報を持つ`Y'`（ルマ）、`Cb`（青色差クロマ）、`Cr`（赤色差クロマ）という成分に分解することができます。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "wvur-gtyxjwG"
      },
      "outputs": [],
      "source": [
        "y, cb, cr = ycbcr[:,:,0], ycbcr[:,:,1], ycbcr[:,:,2]\n",
        "\n",
        "# Y' component\n",
        "plt.figure()\n",
        "plt.imshow(y, cmap='gray')\n",
        "plt.axis('off')\n",
        "plt.show()\n",
        "\n",
        "# Cb component\n",
        "plt.figure()\n",
        "plt.imshow(cb, cmap='gray')\n",
        "plt.axis('off')\n",
        "plt.show()\n",
        "\n",
        "# Cr component\n",
        "plt.figure()\n",
        "plt.imshow(cr, cmap='gray')\n",
        "plt.axis('off')\n",
        "plt.show()"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [
        "Tce3stUlHN0L"
      ],
      "name": "colorspace.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
